package com.framework.dao.rdao;

import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * 基础RedisDao 实现
 * @author ljj
 * @param <T>
 */

@Repository("redisDao")
public class RedisDaoImpl<T> implements RedisDaoI<T>,Serializable {
	private static final long serialVersionUID = 1L;
	@Resource(name = "connectionFactory")
	private  JedisConnectionFactory jedisConnectionFactory;
	
	private  JedisConnection jedisConnection;
	
	private  Jedis jedis;
	/** 
     * 获取一个jedis 客户端 
     * @return 
     */  
//    private Jedis getJedis(){  
//        if(jedis == null){  
//            return jedisConnectionFactory.getShardInfo().createResource();  
//        }  
//        return jedis;  
//    }  
    /** 
     * 通过连接池获取一个jedis 客户端 
     * @return 
     */
    protected  Jedis getJedis() {
        if (jedis == null) {  
               jedisConnection = jedisConnectionFactory.getConnection();  
               jedis = jedisConnection.getNativeConnection(); 
            return jedis;  
        }
        return jedis;
    }  

    /** 
     * 检查是否连接成功 
     * @return 
     */  
    public String ping(){  
        return  this.getJedis().ping();  
    }  
	
    
    /** 
     * 通过key删除（字节） 
     * @param key 
     */  
    public void del(byte [] key){  
        this.getJedis().del(key);
        this.getJedis().close();
    }  
    /** 
     * 通过key删除 
     * @param key 
     */  
    public void del(String key){  
        this.getJedis().del(key);  
    }  
    
    /** 
     * 添加key value 并且设置存活时间(byte) 
     * @param key 
     * @param value 
     * @param liveTime 
     */  
    public void set(byte [] key,byte [] value,int liveTime){  
        this.set(key, value);  
        this.getJedis().expire(key, liveTime);  
    }  
    
    /** 
     * 添加key value 并且设置存活时间 
     * @param key 
     * @param value 
     * @param liveTime 
     */  
    public void set(String key,String value,int liveTime){  
        this.set(key, value);  
        this.getJedis().expire(key, liveTime);  
    }  
    
    /** 
     * 添加key value 
     * @param key 
     * @param value 
     */  
    public void set(String key,String value){  
        this.getJedis().set(key, value);  
    }  
    /**添加key value (字节)(序列化) 
     * @param key 
     * @param value 
     */  
    public void set(byte [] key,byte [] value){  
        this.getJedis().set(key, value);  
    }  
    /** 
     * 获取redis value (String) 
     * @param key 
     * @return 
     */  
    public String get(String key){  
        String value = this.getJedis().get(key);  
        return value;  
    }  
    /** 
     * 获取redis value (byte [] )(反序列化) 
     * @param key 
     * @return 
     */  
    public byte[] get(byte [] key){  
    	
        return this.getJedis().get(key);  
    }  
  
    /** 
     * 通过正则匹配keys 
     * @param pattern 
     * @return 
     */  
    public Set<String> keys(String pattern){  
        return this.getJedis().keys(pattern);  
    }  
  
    /** 
     * 检查key是否已经存在 
     * @param key 
     * @return 
     */  
    public boolean exists(String key){  
        return this.getJedis().exists(key);  
    }  
    /** 
     * 清空redis 所有数据 
     * @return 
     */  
    public String flushDB(){  
        return this.getJedis().flushDB();  
    }  
    /** 
     * 查看redis里有多少数据 
     */  
    public long dbSize(){  
        return this.getJedis().dbSize();  
    }
    
    /**
	 * 追加key value
	 * 
	 * @param key
	 * @param value
	 */
	@Override
	public void append(String key, String value) throws Exception {
		this.getJedis().append(key, value);
	}

	/**
	 * 追加key value (字节)(序列化)
	 * 
	 * @param key
	 * @param value
	 */
	@Override
	public void append(byte[] key, byte[] value) throws Exception {
		this.getJedis().append(key, value);
	}

	@Override
	public void sadd(byte[] key, byte[] value) throws Exception {
		this.getJedis().sadd(key, value);
	}

	/**
	 * lpushx
	 */
	@Override
	public void plull(byte[] key, byte[] value) throws Exception {
		this.getJedis().lpushx(key, value);
	}

	@Override
	public void rpush(byte[] key, byte[] value) throws Exception {
		this.getJedis().rpush(key, value);
	}

	@Override
	public List<byte[]>  lrange(byte[] key, int start, int end) throws Exception {
		List<byte[]> byteList= this.getJedis().lrange(key, start, end);
		return byteList;
	}  
	
	

}
